package com.xsoft.common.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.xsoft.common.CommonInit;

/**
 * created by zhangkai on 2020/11/18
 * email: 405547628@qq.com
 * desc:
 */
public class SharePrefUtils {
    private static final String PREF_NAME = "player_pref";

    public static int getIntValue(String key) {
        return getIntValue(key, 0);
    }

    public static int getIntValue(String key, int defValue) {
        SharedPreferences preferences = CommonInit.context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return preferences.getInt(key, defValue);
    }

    public static float getFloatValue(String key, float defValue) {
        SharedPreferences preferences = CommonInit.context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return preferences.getFloat(key, defValue);
    }

    public static boolean getBooleanValue(String key, boolean defValue) {
        SharedPreferences preferences = CommonInit.context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return preferences.getBoolean(key, defValue);
    }

    public static String getStringValue(String key, String defValue) {
        SharedPreferences preferences = CommonInit.context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return preferences.getString(key, defValue);
    }

    public static void saveValue(String key, Object value) {
        SharedPreferences preferences = CommonInit.context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        if (value instanceof Integer) {
            editor.putInt(key, (Integer) value);
        } else if (value instanceof Boolean) {
            editor.putBoolean(key, (Boolean) value);
        } else if (value instanceof String) {
            editor.putString(key, (String) value);
        } else if (value instanceof Float) {
            editor.putFloat(key, (Float) value);
        } else if (value instanceof Long) {
            editor.putLong(key, (Long) value);
        }
        editor.commit();
    }
}
